let view = null;
const empty = new plus.nativeObj.View("empty", {});
const msgList = [];
let start = false;
let timer = null;

function startEvent() {
	timer = setInterval(() => {
		if (msgList.length) {
			if (!start) {
				start = true;
				msgList[0].fn();
			}
		} else {
			clearInterval(timer)
			timer = null;
		}
	}, 50)
}

const info = function(params = {}) {
	msgList.push({
		fn: showInfo,
		params: params,
		click: params.click
	})
	startEvent();
}

function showInfo() {
	view = new plus.nativeObj.View('view', {
		top: '20px',
		left: '0px',
		height: '100px',
		width: '100%'
	});
	view.setTouchEventRect({
		top: '20px',
		left: '0px',
		width: '100%',
		height: '100px'
	});
	view.drawRect({
		color: '#ffffff',
		borderWidth: '2px',
		radius: '5px',
		borderColor: "#cecece",
		borderWidth: "1px"
	}, {
		top: '5px',
		left: '5%',
		width: '90%',
		height: '90px'
	});
	var richtext = msgList[0].params.template || `
	<img src="static/logo.png" style="width:20px;height:20px;margin-right:4px;vertical-align: bottom;"></img>
	<font style="font-size:16px"> 杨率：</font>
	<br/>
	<a color="#434343" href="href" style="font-size:16px;">这是一条通知消息！</a>
	`;
	view.drawRichText(richtext, {
		top: '20px',
		left: '10%',
		width: '80%',
		height: 'wrap_content'
	})

	var options = {
		type: 'pop-in',
		duration: 500
	};
	var options2 = {
		type: 'pop-out',
		duration: 500
	};

	plus.nativeObj.View.startAnimation(options, empty, view, function() {
		// 关闭原生动画
		view.addEventListener("click", function(e) {
			if (msgList[0].click) {
				msgList[0].click()
			}
		}, false);
		plus.nativeObj.View.clearAnimation();
		view.show()

		setTimeout(() => {
			close()
		}, 3000)
	});
}

const close = function() {
	if (!start) return
	view.animate({
		type: 'shrink',
		frames: 20,
		region: {
			top: '100px',
			bottom: '100px'
		}
	}, function() {
		view.close()
	});
	setTimeout(() => {
		msgList.splice(0, 1)
		start = false;
	}, 500)
}


const message = {
	info,
	close
}

export default message;